package com.ruoyi.web.controller.report;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.report.domain.ReportWorkbookConfig;
import com.ruoyi.report.service.IReportDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


import java.sql.Array;
import java.util.List;

/**
 * @version v1.0
 * @ProjectName: report
 * @ClassName: ReportDataController
 * @Description: 报表数据Controller层
 * @Author: sunyan
 * @Date: 2022/3/2 16:05
 */

@RestController
@RequestMapping("report/data")
public class ReportDataController extends BaseController {
    @Autowired
    IReportDataService reportDataService;

    @GetMapping("/list")
    public TableDataInfo selectReportDataList(ReportWorkbookConfig reportWorkbookConfig){
        startPage();
        //获取用户账户
        String username = SecurityUtils.getUsername();
        //管理员用户查询全部，其他账户只查询自己创建的报表
        if(!username.equals("admin")) {
            reportWorkbookConfig.setCreateBy(username);
        }
        List<ReportWorkbookConfig> list = reportDataService.selectReportDataList(reportWorkbookConfig);
        return getDataTable(list);
    }

    @GetMapping("/{id}")
    public AjaxResult selectReportById(@PathVariable Long id){
        return AjaxResult.success(reportDataService.selectReportById(id));
    }

    @PostMapping
    public AjaxResult insertReportData(@RequestBody ReportWorkbookConfig reportWorkbookConfig){
        return AjaxResult.success(reportDataService.insertReportData(reportWorkbookConfig));
    }

    @PutMapping
    public AjaxResult updateReportData(@RequestBody ReportWorkbookConfig reportWorkbookConfig){
        return AjaxResult.success(reportDataService.updateReportData(reportWorkbookConfig));
    }

    @DeleteMapping("/{id}")
    public AjaxResult deleteReportById(@PathVariable Long id){
        return AjaxResult.success(reportDataService.deleteReportById(id));
    }

    @PutMapping("/star")
    public AjaxResult setReportTemplate(@RequestBody ReportWorkbookConfig reportWorkbookConfig){
        return AjaxResult.success(reportDataService.setReportTemplate(reportWorkbookConfig));
    }

    @PostMapping("/copyReport/{id}")
    public AjaxResult copyReport(@PathVariable("id") Long id){
        ReportWorkbookConfig newConfig = reportDataService.selectReportById(id);
        JSONObject jsonData = JSON.parseObject(newConfig.getReportData());
        String sheetstr = jsonData.getJSONArray("data").toString();
        jsonData.put("data", new Array[]{});
        String s = JSON.toJSONString(jsonData);
        newConfig.setId(null);
        newConfig.setReportName(newConfig.getReportName()+"-副本");
        newConfig.setIsTemplate('0');
        newConfig.setReportData(s);
        newConfig.setSheetData(sheetstr);
        reportDataService.copyInsert(newConfig);
        return  toAjax(true);
    }
}
